from Agent import Agent
import mechanize, re, tempfile, time, logging, sys
from bettybox.Document import Document


class EDFAgent(Agent):
  
  def __init__(self, user, password):
    Agent.__init__(self, "EDF Bleu Ciel")
    self.__user = user
    self.__password = password
    
  def Connect(self):
    cookies = mechanize.CookieJar()
    opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cookies))
    opener.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
    mechanize.install_opener(opener)
    
    print "Connect to EDF Bleu Ciel"
    br = mechanize.Browser()
    self.__br = br
    br.open("http://bleuciel.edf.com/FRONT/NetExpress/services/serviceGetBlocConnexionModule.php?coe_i_id=82834&lang=fr")
    
    br.select_form("connexion")
    br["login"] = self.__user
    br["password"] = self.__password    
    br.submit()
    
    print "Redirect..."
    br.select_form("form1")
    br.submit()
    
    print "Redirect..."
    br.select_form("redirectForm")
    resp = br.submit()
    
    #print resp.info()
    #print resp.read()
    #assert br.viewing_html()
    print "Connection OK"
    
  
  def Collect(self):
    self.Connect()
    print "Begin collect"
    time.sleep(2)
    firstLinks = self.__br.links(url_regex="page_mes_factures", nr=1)
    invoices = self.__collectInvoices(self.__br, firstLinks, [], [])
    for (num, date, content) in invoices:
      attachment = tempfile.NamedTemporaryFile(delete=False)
      attachment.write(content)
      attachment.close()
      print "Wrote temp invoice in: ", attachment.name
      doc = Document()
      doc.title = 'Facture EDF du ' + date
      doc.attachment = attachment.name
      doc.tags += [ 'edf' ]
      yield doc
      
  
  def __collectInvoices(self, br, links, previous_links, invoiceIds):
    invoices = []
    if self.checkLoop(previous_links):
      for link in links:
        print "Parse: ", link
        time.sleep(2)
        resp = br.follow_link(link)
        links = filter(lambda x:True, br.links(url_regex="getFacturePDF.do"))
        for invoiceLink in links:
          invoiceContent = self.__getPdf(br, invoiceLink)
          if not invoiceContent is None:
            invoiceId = re.search("numFact=(\d+)&", invoiceLink.url).group(1)
            if not invoiceId in invoiceIds:
              invoiceDate = re.search("dateFactureQE=(\d\d/\d\d/\d\d\d\d)", invoiceLink.url).group(1)
              invoices += [ (invoiceId, invoiceDate, invoiceContent) ]
              invoiceIds += [ invoiceId ]
              print "Found invoice: ", invoiceId, invoiceDate
        invoices += self.__collectInvoices(br, filter(lambda x:True, br.links(url_regex="mes_factures(.*)annee")), previous_links + [resp.geturl()], invoiceIds)
    return invoices
  
  def __getPdf(self, br, link):
    resp = br.follow_link(link)
    time.sleep(2)
    content = None
    if resp.info()["Content-type"] == 'application/pdf':
      content = resp.read()
    br.back()
    return content
  
  
if __name__ == '__main__':
  logger = logging.getLogger("mechanize")
  logger.addHandler(logging.StreamHandler(sys.stdout))
  logger.setLevel(logging.DEBUG)
  
  agent = EDFAgent('fournierg@gmail.com', 'xxxxxxxxxx')
  #agent.Collect()
  #agent.Connect()
